package com.frame.common.tools.util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.UUID;

/**
 * @auther zhangwj
 * @date 2021/3/24 下午4:23
 */
public class MDFiveUtils {

    private static final String SLAT = "&%5123***&&%%$$#@";

    public static String MD5(String str) {
        try {
            String base = str + "/" + SLAT;
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(base.getBytes(StandardCharsets.UTF_8));

            byte[] bytes = messageDigest.digest();
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                result.append(Integer.toHexString((0x000000FF & b) | 0xFFFFFF00).substring(6));
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

    public static String getOnlyId() {
        String uuid = UUID.randomUUID() + "/" + System.currentTimeMillis();
        return Objects.requireNonNull(MD5(uuid)).toUpperCase();
    }
}
